package utils

import (
	"blog-back-end/models/user"
	"fmt"
	"github.com/spf13/viper"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
)

func InitialViperConfog() {
	viper.SetConfigFile("config.yml")

	err := viper.ReadInConfig()
	if err != nil {
		fmt.Println("读取失败")
		return
	} else {
		fmt.Println("viper读取成功")

	}

}

var DB *gorm.DB

func InitialDbConfig() {

	db, err := gorm.Open(mysql.Open(viper.GetString("mysql.dsn")), &gorm.Config{
		NamingStrategy: schema.NamingStrategy{
			SingularTable: true,
		},
	})
	if err != nil {
		fmt.Println("数据库连接失败")
		return
	} else {
		fmt.Println("数据库连接成功")
		DB = db

		err := DB.AutoMigrate(&user.User{})
		if err != nil {
			return
		}
	}

}

// TODO 全局日志配置
func LogConfig() {
	//logrus.SetLevel()
}
